package com.yingsheng.bi.hks.service.user;

import com.yingsheng.bi.hks.basic.PUser;
import com.yingsheng.bi.hks.basic.exception.ServiceRuntimeException;
import com.yingsheng.bi.hks.basic.utils.Subject;
import com.yingsheng.bi.hks.dto.user.BaseInfoModReq;
import com.yingsheng.bi.hks.dto.user.BaseInfoModRes;
import com.yingsheng.bi.hks.repository.dao.po.RecommenderInfo;
import com.yingsheng.bi.hks.repository.dao.po.Users;
import com.yingsheng.bi.hks.repository.dao.service.RecommenderInfoDaoService;
import com.yingsheng.bi.hks.repository.dao.service.UsersDaoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class UsersMgmtService {

    @Autowired
    private UsersDaoService usersDaoService;

    @Autowired
    private RecommenderInfoDaoService recommenderInfoDaoService;

    public BaseInfoModRes baseInfoMod(BaseInfoModReq req) {

        if (req.getRecommendationId() != null) {
            RecommenderInfo recommenderInfo = recommenderInfoDaoService.selectById(req.getRecommendationId());
            if (recommenderInfo == null) {
                throw new ServiceRuntimeException("推荐人不存在！");
            }
        }

        PUser user = Subject.get();
        Users users = usersDaoService.selectByUserId(user.getId());
        if (users.getRecommendationId() != null && req.getRecommendationId()!=null){
            throw new ServiceRuntimeException("推荐人不可更改！");
        }
        users.setNickname(req.getNickname());
        users.setAvatar(req.getAvatar());
        users.setRecommendationId(req.getRecommendationId());
        usersDaoService.updateById(users);
        BaseInfoModRes baseInfoModRes = new BaseInfoModRes();
        return baseInfoModRes;
    }
}
